//
//  VideoTableViewController.m
//  
//
//  Created by admin on 17/3/2.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "VideoTableViewController.h"
#import <AFNetworking.h>
#import "NetworkTool.h"
#import <YYModel.h>
#import "NewsModel.h"
#import <YYModel.h>
#import "VideoModel.h"
#import "VideoCell.h"
#import "NSDictionary+Data.h"


//视频
#import "XMPlayer.h"
#import "XMPlayerTableViewCell.h"
#import "XMPlayerModel.h"

@interface VideoTableViewController ()

@end

@implementation VideoTableViewController
{
    /// 数据源数组
    NSArray *_dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    A = 0;
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"VideoCell" bundle:nil] forCellReuseIdentifier:@"VideoCell"];
    ///当我们的数据未能显示满一屏幕的时候，UITableView会显示多余的横线
    self.tableView.tableFooterView = [[UIView alloc] init];

    self.arrayData = [[NSMutableArray alloc]init];
    self.arrayMutPaly = [[NSMutableArray alloc]init];

    //诗歌歌谱的上下拉
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        A = 1;
        self.numberPage = 0;
        //诗歌歌谱的分类
        [self addDataDic :self.numberPage];
        [self.tableView.mj_header endRefreshing];
    }];
    // 上拉刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        A = 1;
        self.numberPage++;
        //诗歌歌谱的分类
        [self addDataDic:self.numberPage];
        [self.tableView.mj_footer endRefreshing];
    }];
}
-(void)setCid:(NSString *)cid
{
    _cid = cid;
    [self addDataDic:0];
}
-(void)addDataDic :(NSInteger)numberPage
{
    NSString * fristStr = [NSString stringWithFormat:@"%.2f",theminBloodSugar];
    NSString * lastStr = [NSString stringWithFormat:@"%.2f",themaxBloodSugar];;
    __weak typeof(self) weakSelf = self;
    [LSZRequestResolutionDic initWithVideoDetailChooseRequestData:_cid last_id:lastStr first_id:fristStr :^(BOOL isOK, NSString * str, LSZDetailsVideoSelectionManager * mg) {
        if (isOK == YES) {
//            [weakSelf.arrayData addObjectsFromArray:mg.mutDataArray];
            if (mg.mutDataPlayArray.count != 0){
                [weakSelf setDetailsVidelManager:mg :numberPage];
            }
        }else{
            NSString * string  = [NSString stringWithFormat:@"%@_%@",videoChoose,_cid];
            [LSZCacheHandling initWithfindPlist:string :^(BOOL isOk, NSDictionary * diction) {
                if (isOk == YES) {
                    LSZDetailsVideoSelectionManager * manager = [[LSZDetailsVideoSelectionManager alloc]initWithDetailsVideoSelectionManager:diction];
                    if (manager.mutDataPlayArray.count != 0){
                        [weakSelf setDetailsVidelManager:manager :numberPage];
                    }
                }else{
                    [weakSelf setHubStr:str];
                }
            }];
//            if ([str isEqualToString:NoNetwork])
//            {
//
//            }else{
//                [weakSelf setHubStr:str];
//            }
        }
    }];
}
-(void)setDetailsVidelManager :(LSZDetailsVideoSelectionManager *)mg  :(NSInteger)numberPage
{
    if (numberPage == 0 ) {
        [self.arrayData removeAllObjects];
        [self.arrayData addObjectsFromArray:mg.mutDataArray];
        
        //
        [self.arrayMutPaly removeAllObjects];
        [self.arrayMutPaly addObjectsFromArray:mg.mutDataPlayArray];
        self.dataArray = [XMPlayerModel mj_objectArrayWithKeyValuesArray:self.arrayMutPaly];
    }else{
        [self.arrayData addObjectsFromArray:mg.mutDataArray];
        [self.arrayMutPaly addObjectsFromArray:mg.mutDataPlayArray];
        self.dataArray = [XMPlayerModel mj_objectArrayWithKeyValuesArray:self.arrayMutPaly];
    }
    [self CompareTheMaximumAndTheMinimum];
    [self.tableView reloadData];
}
-(void)setHubStr :(NSString *)str
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;//枚举类型不同的效果
    //    hud.mode = MBProgressHUDModeCustomView;
    hud.label.text= [NSString stringWithFormat:@"%@",str];
    [self performSelector:@selector(hiddenMBAction) withObject:nil afterDelay:1.0];
}
-(void)hiddenMBAction
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}
#pragma mark-  比较最大值和最小值
-(void)CompareTheMaximumAndTheMinimum
{
    for (int i = 0; i < self.arrayData.count; ++i) {
        LSZDetailsVideoSelectionMg * mg = nil;
        mg = self.arrayData[i];
        NSString *thetmpHistory = [NSString stringWithFormat:@"%@",mg.id_id];
        //bloodSugar
        NSLog(@"当前值>>>>%@",thetmpHistory);
        if (i == 0 && A== 0) {
            themaxBloodSugar = thetmpHistory.floatValue;
            theminBloodSugar = thetmpHistory.floatValue;
        }
        //最大值
        if (thetmpHistory.floatValue - themaxBloodSugar > 0.000001)
        {
            themaxBloodSugar = thetmpHistory.floatValue;
            NSLog(@"%f>>>最大值",themaxBloodSugar);
        }
        //最小值
        if (theminBloodSugar - thetmpHistory.floatValue >0.000001) {
            theminBloodSugar = thetmpHistory.floatValue;
            NSLog(@"%f>>>最小值",theminBloodSugar);
        }
    }
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return self.arrayData.count;
    return self.dataArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMPlayerTableViewCell *cell = [XMPlayerTableViewCell cellWithTableView:tableView cellForRowAtIndexPath:indexPath];
    XMPlayerModel *model = self.dataArray[indexPath.row];
    cell.playerModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
//    VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VideoCell" forIndexPath:indexPath];
//    LSZDetailsVideoSelectionMg * mg = nil;
//    mg = self.arrayData[indexPath.row];
//    cell.video = mg;
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMPlayerModel *model = self.dataArray[indexPath.row];

    CGFloat cellH = [self initWithStrHeigh:model.title];
    
    return (cellH+260);
}

-(CGFloat)initWithStrHeigh :(NSString * )str
{
    if (ScreenHeight == 667 || ScreenHeight == 736) {
        CGRect labTitleRect = [str boundingRectWithSize:CGSizeMake(ScreenWidth-20, ScreenHeight*9) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:[NSDictionary dictionaryWithObject:FONT_18 forKey:NSFontAttributeName] context:nil];
        return labTitleRect.size.height+15;
    }else{
        CGRect labTitleRect = [str boundingRectWithSize:CGSizeMake(ScreenWidth-20, ScreenHeight*9) options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:[NSDictionary dictionaryWithObject:FONT_16 forKey:NSFontAttributeName] context:nil];
        return labTitleRect.size.height+15;
    }
}
 
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    LSZDetailsVideoSelectionMg * video =self.arrayData[indexPath.row];
//    LSZVideoDetailsWebView * viewController = [[LSZVideoDetailsWebView alloc] init];
//    viewController.url = video.url;
//    viewController.titleStr = @"视频";
//    viewController.isDistinguish = @"1";
//    [self.navigationController pushViewController:viewController animated:YES];
//}
@end
